// ==UserScript==
// @name           Build-in Attachment Reminder Fixer
// @namespace      http://www.quchao.com/entry/build-in-attachment-reminder-fixer-for-thunderbird
// @description    It fixes the build-in attachment reminder, and will remind you if you likely wanted to attach something but you didn't.
// @include        chrome://messenger/content/messengercompose/messengercompose.xul
// @compatibility  Thunderbird 3.0.*
// @author         Qu Chao (Chappell.Wat) <Chappell.Wat@Gmail.com>
// @version        1.0
// @Note
// ==/UserScript==
// ver 1.0 @ 2009-07-12
//  Initialize release

(function() {
	// shortcut
	var prefs = Components.classes['@mozilla.org/preferences-service;1'].getService(Components.interfaces.nsIPrefBranch);

	// get keywords
	var keywords = prefs.getComplexValue('mail.compose.attachment_reminder_keywords', Components.interfaces.nsIPrefLocalizedString).data;
	if ('' !== keywords) {
		// turn off the build-in attachment-reminder function
		prefs.setBoolPref('mail.compose.attachment_reminder', false);

		// get pattern for the keywords
		var keywordsPattern = keywords.replace(/(\.|\$|\^|\{|\[|\(|\||\)|\*|\+|\?|\\|\"|\')/g, '\\$1').replace(/,/g, '|');

		// cleaning
		keywords = prefs = null;

		// attachment checking
		var checkAttachment = function () {
			// if there's an attachment at least
			if (0 < document.getElementById('attachmentBucket').itemCount) {
				return true;
			}

			// preparing data
			var textArr = [],
				mailSubject = document.getElementById('msgSubject').value,
				mailContent = document.getElementById('content-frame').contentDocument,
				textNodes = mailContent.evaluate(
				'//text()[not(ancestor::blockquote)]',
				mailContent.documentElement,
				null,
				XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE,
				null
			);
			for (var i = textNodes.snapshotLength - 1; 0 <= i; i --) {
				textArr.push(textNodes.snapshotItem(i).data);
			}
			textArr.push(mailSubject);

			// cleaning
			mailSubject = mailContent = textNodes = null;

			// checking
			if (true === (new RegExp(keywordsPattern, 'i')).test(textArr.toString())) {
				var bundle = document.getElementById('bundle_composeMsgs');
				return 0 === gPromptService.confirmEx(
					null,
					bundle.getString('attachmentReminderTitle'),
					bundle.getString('attachmentReminderMsg'),
					gPromptService.BUTTON_POS_0 * gPromptService.BUTTON_TITLE_IS_STRING + gPromptService.BUTTON_POS_1 * gPromptService.BUTTON_TITLE_IS_STRING,
					bundle.getString('attachmentReminderFalseAlarm'),
					bundle.getString('attachmentReminderYesIForgot'),
					null,
					null,
					{value: 0}
				);
			}
			return true;
		};

		// hacking the related functions
		var SendMessageOriginal = SendMessage;
		SendMessage = function() {
			if (false === checkAttachment()) {
				return false;
			} else {
				SendMessageOriginal.apply(this, arguments);
			}
		};

		var SendMessageWithCheckOriginal = SendMessageWithCheck;
		SendMessageWithCheck = function() {
			if (false === checkAttachment()) {
				return false;
			} else {
				SendMessageWithCheckOriginal.apply(this, arguments);
			}
		};

		var SendMessageLaterOriginal = SendMessageLater;
		SendMessageLater = function() {
			if (false === checkAttachment()) {
				return false;
			} else {
				SendMessageLaterOriginal.apply(this, arguments);
			}
		};
	}
})();